package elitech.vietnam.weatherfashion.tabs.settings;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.widget.NotifyButton;

public class FriendListChiaLam2Fragment extends AbstractFragment implements View.OnClickListener {

	private Button btnBack;

	public static final String TAB_TUI = "TUI";
	public static final String TAB_BAN = "BAN";

	private FragmentTabHost mTabHost;
	private NotifyButton button_notification;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_friend_list_chia_lam_2, null);
		setupView(view);
		button_notification = (NotifyButton) view.findViewById(R.id.button_notification);
		
		((MainActivity) getActivity()).setCurrentNotifyButton(button_notification);
		MemberDTO m = GlobalValue.getInstance(getActivity()).readUserInfo();
		if (m == null)
			button_notification.setVisibility(View.GONE);
		else 
			button_notification.setVisibility(View.VISIBLE);
		//button_notification.setOnClickListener(this);

		initView(view);
		mTabHost = (FragmentTabHost) view.findViewById(R.id.friendListTabHost);
		initTabs();

		return view;
	}

	private void initView(View view) {
		/****************** s **************/
		btnBack = (Button) view.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				getActivity().onBackPressed();
			}
		});

	}

	// @Override
	// public void onResume() {
	// super.onResume();
	// mTabHost.setCurrentTab(0);
	// }

	private void initTabs() {
		mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.friendListTabContent);

		Bundle bundle = new Bundle();
		bundle.putInt("list_friend", 1);
		mTabHost.addTab(mTabHost.newTabSpec(TAB_TUI)
				.setIndicator(createTabView(getActivity(), R.drawable.meadded_s, R.drawable.meadded_d, getString(R.string.setting_friend_list_tui_them_ban), true)), FriendListTabFragment.class,
				bundle);
		bundle = new Bundle();
		bundle.putInt("list_friend", 2);
		mTabHost.addTab(
				mTabHost.newTabSpec(TAB_BAN)
						.setIndicator(createTabView(getActivity(), R.drawable.addedme_s, R.drawable.addedme_d, getString(R.string.setting_friend_list_ban_them_tui), true)),
				FriendListTabFragment.class, bundle);

	}

	private static View createTabView(final Context context, int selected, int normal, String title, boolean isLoggedIn) {

		View view = LayoutInflater.from(context).inflate(R.layout.styler_tab, null);
		TextView item_title = (TextView) view.findViewById(R.id.styler_childtab_itemtext);
		ImageView item_image = (ImageView) view.findViewById(R.id.styler_childtab_itemimage);
		Drawable drawable = Utils.getTabDrawable(context, selected, normal);
		item_title.setText(title);
		item_image.setImageDrawable(drawable);
		if (!isLoggedIn)
			view.setVisibility(View.GONE);

		return view;
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_notification:
			Log.w("Notification", "Num Count: " + WFApplication.getInstance().mListNoti.size());
			((MainActivity) getActivity()).mNotiWindow.showAsDropDown(button_notification);
			((MainActivity) getActivity()).mNotiWindow.notifyDataChanged();
			button_notification.setNumber(0);
			break;

		default:
			break;
		}
	}

}
